package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcHuaLongRosterMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcHuaLongRoster;
import com.csun.cmny.provider.model.domain.OmcHuaLongRosterItem;
import com.csun.cmny.provider.model.dto.hualong.HuaLongRosterDto;
import com.csun.cmny.provider.model.dto.hualong.HuaLongRosterItemDto;
import com.csun.cmny.provider.model.dto.hualong.HuaLongRosterQueryDto;
import com.csun.cmny.provider.model.dto.hualong.HuaLongRosterShiftDto;
import com.csun.cmny.provider.model.vo.hualong.HuaLongRosterVo;
import com.csun.cmny.provider.service.OmcHuaLongRosterItemService;
import com.csun.cmny.provider.service.OmcHuaLongRosterService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-10-29 11:54
 * @Description:
 */
@Service
@Transactional
public class OmcHuaLongRosterServiceImpl extends BaseService<OmcHuaLongRoster> implements OmcHuaLongRosterService {

    @Resource
    private OmcHuaLongRosterMapper omcHuaLongRosterMapper;
    @Resource
    private OmcHuaLongRosterItemService omcHuaLongRosterItemService;

    /**
     * 主管排班
     *
     * @param huaLongRosterDto the hua long roster dto
     * @param loginAuthDto     the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer save(HuaLongRosterDto huaLongRosterDto, LoginAuthDto loginAuthDto) {

        OmcHuaLongRoster omcHuaLongRoster = new OmcHuaLongRoster();
        omcHuaLongRoster.setUpdateInfo(loginAuthDto);
        omcHuaLongRoster.setGroupId(loginAuthDto.getGroupId());
        omcHuaLongRoster.setTeamId(huaLongRosterDto.getTeamId());
        omcHuaLongRoster.setTeamGroup(huaLongRosterDto.getTeamGroup());
        omcHuaLongRoster.setTeamBuildName(huaLongRosterDto.getTeamBuildName());
        omcHuaLongRoster.setMonth(huaLongRosterDto.getMonth());
        omcHuaLongRoster.setNurseNum(huaLongRosterDto.getHuaLongRosterItemDtoList().size());

        Integer result = omcHuaLongRosterMapper.insertSelective(omcHuaLongRoster);

        //****************构建护工班次****************//
        List<OmcHuaLongRosterItem> omcHuaLongRosterItemList = Lists.newArrayList();
        for (HuaLongRosterItemDto hlrid : huaLongRosterDto.getHuaLongRosterItemDtoList()) {
            for (HuaLongRosterShiftDto hlrsd : hlrid.getHuaLongRosterShiftDtoList()) {
                OmcHuaLongRosterItem item = new OmcHuaLongRosterItem();
                item.setUpdateInfo(loginAuthDto);
                item.setGroupId(loginAuthDto.getGroupId());
                item.setRosterId(omcHuaLongRoster.getId());
                item.setNurseId(hlrid.getNurseId());
                item.setNurseName(hlrid.getNurseName());
                item.setWeek(hlrsd.getWeek());
                item.setDay(hlrsd.getDay());
                item.setShift(hlrsd.getShift());
                item.setIsStrong(hlrsd.getIsStrong());
                omcHuaLongRosterItemList.add(item);
            }
        }

        result += omcHuaLongRosterItemService.batchSave(omcHuaLongRosterItemList);

        return result;
    }

    /**
     * 主管排班列表
     *
     * @param huaLongRosterQueryDto the hua long roster query dto
     *
     * @return the list.
     */
    @Override
    public List<HuaLongRosterVo> queryListWithPage(HuaLongRosterQueryDto huaLongRosterQueryDto) {

        PageHelper.startPage(huaLongRosterQueryDto.getPageNum(), huaLongRosterQueryDto.getPageSize());

        return omcHuaLongRosterMapper.queryListWithPage(huaLongRosterQueryDto);
    }

    /**
     * 查看详情
     *
     * @param id the id
     *
     * @return the hua long roster vo
     */
    @Override
    public HuaLongRosterVo queryDetailById(Long id) {

        // 根据Id查询
        OmcHuaLongRoster omcHuaLongRoster = omcHuaLongRosterMapper.selectByPrimaryKey(id);
        if (PublicUtil.isEmpty(omcHuaLongRoster)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10090001);
        }

        HuaLongRosterVo huaLongRosterVo = new HuaLongRosterVo();
        BeanUtils.copyProperties(omcHuaLongRoster, huaLongRosterVo);
        huaLongRosterVo.setTeamId(omcHuaLongRoster.getTeamId());

        List<HuaLongRosterItemDto> huaLongRosterItemDtoList = omcHuaLongRosterMapper.queryDetailById(id);
        huaLongRosterVo.setHuaLongRosterItemDtoList(huaLongRosterItemDtoList);

        return huaLongRosterVo;
    }

    /**
     * 根据Id删除排班
     *
     * @param id    the hua long roster id
     *
     * @return  the int.
     */
    @Override
    public Integer deleteById(Long id) {

        return omcHuaLongRosterMapper.deleteById(id);
    }

    /**
     * 主管调班
     *
     * @param huaLongRosterDto the hua long roster dto
     * @param loginAuthDto     the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer change(HuaLongRosterDto huaLongRosterDto, LoginAuthDto loginAuthDto) {

        Long rosterId = huaLongRosterDto.getId();
        if (PublicUtil.isEmpty(rosterId)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10090002);
        }

        // 根据Id查询排班计划是否存在
        OmcHuaLongRoster omcHuaLongRoster = omcHuaLongRosterMapper.selectByPrimaryKey(rosterId);
        if (PublicUtil.isEmpty(omcHuaLongRoster)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10090001);
        }

        /*************** 方案一 ***************/
        /**
         * 保存调班的记录，保留调班之前的记录
         *
         * 更新当前的排班，记录调班记录
         */

        /*************** 方案二 ***************/
        /**
         * 直接更新当前排班
         */
        OmcHuaLongRosterItem omcHuaLongRosterItem = new OmcHuaLongRosterItem();
        omcHuaLongRosterItem.setRosterId(rosterId);
        Integer result = omcHuaLongRosterItemService.delete(omcHuaLongRosterItem);

        //****************构建护工班次****************//
        List<OmcHuaLongRosterItem> omcHuaLongRosterItemList = Lists.newArrayList();
        for (HuaLongRosterItemDto hlrid : huaLongRosterDto.getHuaLongRosterItemDtoList()) {
            for (HuaLongRosterShiftDto hlrsd : hlrid.getHuaLongRosterShiftDtoList()) {
                OmcHuaLongRosterItem item = new OmcHuaLongRosterItem();
                item.setId(hlrsd.getId());
                item.setUpdateInfo(loginAuthDto);
                item.setGroupId(loginAuthDto.getGroupId());
                item.setRosterId(rosterId);
                item.setNurseId(hlrid.getNurseId());
                item.setNurseName(hlrid.getNurseName());
                item.setWeek(hlrsd.getWeek());
                item.setDay(hlrsd.getDay());
                item.setShift(hlrsd.getShift());
                item.setIsStrong(hlrsd.getIsStrong());
                omcHuaLongRosterItemList.add(item);
            }
        }

        result += omcHuaLongRosterItemService.batchSave(omcHuaLongRosterItemList);

        return result;
    }
}
